package pages

import (
	"net/http"
	"note-go/session"
	"note-go/util"
	"runtime"

	"github.com/gin-gonic/gin"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/mem"
)

func IndexPage(c *gin.Context) {
	sessionID, _ := c.Cookie("session_id")
	username, _ := session.GetSession(sessionID)
	cpuInfo, err := cpu.Info()
	if err != nil {
		return
	}
	var _cpuModelName = cpuInfo[0].ModelName
	var _os = runtime.GOOS
	var _arch = runtime.GOARCH
	var _osVersion string

	switch runtime.GOOS {
	case "darwin":
		fallthrough
	case "linux":
		_osVersion = util.GetOSPrettyName()
	case "windows":
		_osVersion = util.GetOSPrettyName()
	default:
		_osVersion = "Unsupported systems."
	}

	vmStat, _ := mem.VirtualMemory()

	var _totalMemory = vmStat.Total / 1024 / 1024
	var _availableMemory = vmStat.Available / 1024 / 1024

	c.HTML(http.StatusOK, "index.html", gin.H{
		"title":            "Note Go",
		"os":               _os,
		"os_version":       _osVersion,
		"arch":             _arch,
		"total_memory":     _totalMemory,
		"available_memory": _availableMemory,
		"cpu_model_name":   _cpuModelName,
		"selected_item":    "index",
		"username":         username,
		"name":             "admin",
		"is_login":         true,
	})
}
